package com.yuanfeng.job.job;


import cn.hutool.core.util.StrUtil;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.dto.order.CancelParaEntityDTO;
import com.yuanfeng.commoms.dto.order.WapGoodsSaleSumDTO;
import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.job.feign.ActivityServiceClient;
import com.yuanfeng.job.feign.BaseServiceClient;
import com.yuanfeng.job.feign.GoodsServiceClient;
import com.yuanfeng.job.feign.PayMentServiceClient;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @Author Dark Wang
 * @Description 此方法用于更新秒杀未支付订单状态并返还秒杀库存
 * @Date 09:12 2022/10/31
 * @Param
 * @return
 **/
public class UpdateSeckillOrderBaseStateJob implements BaseJob {

	private static final Logger logger = LoggerFactory.getLogger(UpdateSeckillOrderBaseStateJob.class);

	@Autowired
	UpdateOrderStatusService UpdateOrderStatusService;

	@Autowired
	PayMentServiceClient payMentServiceClient;

	@Autowired
	GoodsServiceClient goodsServiceClient;

	@Autowired
	BaseServiceClient baseServiceClient;

	@Autowired
	ActivityServiceClient activityServiceClient;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		List<String> orderIdList = UpdateOrderStatusService.queryUnpaidOrdersToBeCancelled();
		Integer orderCancelIdentity = 3;
		for(String orderId:orderIdList) {
			// 订单取消状态
			Integer orderStatus = 7;
//			WapGoodsSaleSumUpdateVO wgssu = new WapGoodsSaleSumUpdateVO();
//			Integer goodsType = 0;
			// 查询商品信息中商品id,商品数量，commonid
//			List<AddInventoryDTO> aie = UpdateOrderStatusService.queryGoodsDetails(orderId);
//            for (AddInventoryDTO addInventoryDTO : aie) {
//                wgssu.setGoodsId(addInventoryDTO.getGoodsId());
//                wgssu.setCommonId(addInventoryDTO.getCommonId());
//                if (addInventoryDTO.getOrderPromotionId() == 6) {
//                    goodsType = 1;
//                }
//                wgssu.setGoodsType(goodsType);
//                if (addInventoryDTO.getCommonSalenum() - addInventoryDTO.getOrderGoodsNum() <= 0) {
//                    wgssu.setGoodsNum(0);
//                } else {
//                    wgssu.setGoodsNum(addInventoryDTO.getCommonSalenum() - addInventoryDTO.getOrderGoodsNum());
//                }
//                UpdateOrderStatusService.updateSaleSum2(wgssu);
//            }
			// 订单取消原因
			String orderCancelReason = "未在规定时间内支付";
			// 订单取消时间
			String orderCancelDate = DateUtils.getNowDateToString();
			boolean isUpdateSuccess = UpdateOrderStatusService.updateByOrderBase(orderId, orderStatus,
					orderCancelIdentity, orderCancelReason, orderCancelDate);
			boolean isUpdateGoodsSuccess = UpdateOrderStatusService.updateByOrderGoods(orderId, orderStatus);
			//取消佣金订单
			UpdateOrderStatusService.updateOrderStatus(CommonType.CommissionOrderStatus.CANCEL.getCode(), orderId);
			payMentServiceClient.updateCancelStatus(orderId);

			// 查询支付单号
			CancelParaEntityDTO cancelParaEntity = UpdateOrderStatusService.queryPaymentNumberAndRedpacketId(orderId);
			String paymentNumber = cancelParaEntity.getPaymentNumber();
			String redpacketId = cancelParaEntity.getRedpacketCode();
			String voucherCode = cancelParaEntity.getVoucherCode();
			String platformCouponCode = cancelParaEntity.getPlatformCouponCode();
			String markId = cancelParaEntity.getOrderBuyId();

			//取消订单成功后 修改库存
			//获取goodsid commonid 购买数量
			List<WapGoodsSaleSumDTO> wapGood = UpdateOrderStatusService.queryGoodsXinXi(orderId);
			goodsServiceClient.updateGoodsStock(wapGood);
			//退还红包
			if (redpacketId != null) {
				// 查询订单状态
				List<Integer> orderStatusList = UpdateOrderStatusService.queryOrderStatus(paymentNumber);
				Map<String, Object> codeMap = new HashMap<String, Object>();
				codeMap.put("redpacketId", redpacketId);
				codeMap.put("platformVoucherCode", platformCouponCode);
				codeMap.put("type", "0");
				Set set = new HashSet(orderStatusList);
				if (set.size() == 1) {
					activityServiceClient.updateSalesPrm(codeMap);
					if (!StrUtil.isEmptyIfStr(voucherCode)) {
						List<String> voucherCodeList = Arrays.asList(voucherCode.split(","));
						Map<String, Object> voucherCodeMap = new HashMap<String, Object>();
						voucherCodeMap.put("voucherId", voucherCodeList);
						voucherCodeMap.put("type", "0");
						activityServiceClient.updateSalesPrm(voucherCodeMap);
					}
				}
			} else {
				if (!StrUtil.isEmptyIfStr(voucherCode)) {
					List<String> voucherCodeList = Arrays.asList(voucherCode.split(","));
					Map<String, Object> voucherCodeMap = new HashMap<String, Object>();
					voucherCodeMap.put("voucherId", voucherCodeList);
					voucherCodeMap.put("platformVoucherCode", platformCouponCode);
					voucherCodeMap.put("type", "0");
					activityServiceClient.updateSalesPrm(voucherCodeMap);
				}
			}
		}


	}
}
